
<template>
  <div class="employees-container">
    <div class="app-container">
      <employeedialog>
        <template #left>
          <span>总记录数:{{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/import')">excel导入</el-button>
          <el-button type="danger" size="small" @click="handleDownload">excel导出</el-button>
          <el-button type="primary" size="small" @click="showdialog=true">新增员工</el-button>
        </template>
      </employeedialog>

      <el-card style="margin-top: 10px;">
        <el-table :default-sort="{prop: 'date', order: 'ascending '}" :data="list" border>
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像">
            <template #default="{row}">
              <img :src="row.staffPhoto" alt="" style="width:100%">
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              {{ getemm(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column sortable label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail?id=${row.id}`)">查看</el-button>
              <el-button type="text" size="small" @click="getlistlist(row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="hdel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="query.page"
            :page-sizes="[2, 5, 8, 10]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <el-dialog :visible.sync="showdialog" title="弹窗" @close="resetyu">
        <employeelog ref="diolp" @success="hsuccess" @close="resetdd" />
      </el-dialog>
      <el-dialog :visible.sync="showroledialog" title="弹窗">
        <assignRole v-if="showroledialog" :id="curid" @hsuccess="successss" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import employeelog from '@/views/employees/employeedialog.vue'
import { getsememm, delsememm } from '@/api/employees'
import emmlist from '@/constant/employees'
import assignRole from '@/views/employees/assignRole.vue'
const hireType = {}
emmlist.hireType.forEach(item => {
  hireType[item.id] = item.value
})
export default {
  name: 'VueHrEmployees',
  components: {
    employeelog,
    assignRole
  },
  data() {
    return {
      curid: '',
      hireType: emmlist.hireType,
      showdialog: false,
      showroledialog: false,
      query: {
        page: 1,
        size: 2
      },
      list: [],
      total: 0
    }
  },

  mounted() {

  },
  created() {
    this.loadlist()
  },

  methods: {
    async loadlist() {
      try {
        const res = await getsememm(this.query)
        console.log(res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    getemm(id) {
      return hireType[id]
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.query.size = val
      this.query.page = 1
      this.loadlist()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)

      this.query.page = val
      this.loadlist()
    },
    resetdd() {
      this.showdialog = false
    },
    hsuccess() {
      this.total++
      const maxpage = Math.ceil(this.total / this.query.size)
      this.query.page = maxpage
      this.showdialog = false
      this.loadlist()
    },
    resetyu() {
      this.$refs.diolp.resettt()
    },
    hdel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          if (this.list.length === 1) {
            this.query.page--
            if (this.query.page <= 0) {
              this.query.page = 1
            }
          }
          const res = await delsememm(id)
          this.loadlist()
          console.log('delsememm', res)
        // 保存数据
        } catch (err) {
          console.log('delsememm', err)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    formatdata(list) {
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      const one = list[0]
      if (!one) return
      const header = Object.keys(one).map(enkey => {
        return map[enkey]
      })
      const data = list.map(item => {
        const key = item.formOfEmployment
        item['formOfEmployment'] = hireType[key] || '未知'
        return Object.values(item)
      })
      return { header: header, data: data }
    },
    async handleDownload() {
      try {
        const res = await getsememm(this.query)
        const { header, data } = this.formatdata(res.data.rows)
        import('@/vendor/Export2Excel').then(excel => {
          // excel表示导入的模块对象
          console.log(excel)
          excel.export_json_to_excel({
            header: header, // 表头 必填
            data: data, // 具体数据 必填
            filename: 'excel-list', // 文件名称
            autoWidth: true, // 宽度是否自适应
            bookType: 'xlsx' // 生成的文件类型
          })
        })
        // 保存数据
      } catch (err) {
        console.log('getsememm', err)
      }
    },
    successss() {
      this.showroledialog = false
    },
    getlistlist(id) {
      this.showroledialog = true
      this.curid = id
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
